{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 下载位置"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# import机制"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 标准`import`\n",
    "Python中所有加载到内存的模块都放在sys.modules中。当import一个模块时，会发生：\n",
    "+ 首先会在sys.modules列表中查找是否以及加载了此模块，如果加载了，则只是将模块的名字加入到正在调用`import`的模块的Local名字空间中；\n",
    "+ 如果没有加载则从`sys.path`目录中按照模块名称查找模块文件（模块可以是py,pyc,pyd）找到后将模块加载到内存，并加到sys.modules中，并将名字导入当前模块的Local名字空间中\n",
    "\n",
    "**一个模块不会重复载入。** 多个不同模块都可以用`import`引入同一个模块到自己的Local名字空间，其实背后的PyModuleObject对象只有一个。\n",
    "\n",
    "`import`只能导入模块，不能导入模块中的对象（类，函数，变量），要导入只能`from moduleA import classA`"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import sys\n",
    "sys.path"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['/home/yinzp/gitee/yinzp_zone/contents/Linux learning',\n",
       " '/home/yinzp/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/pythonFiles',\n",
       " '/home/yinzp/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/pythonFiles/lib/python',\n",
       " '/usr/lib/python36.zip',\n",
       " '/usr/lib/python3.6',\n",
       " '/usr/lib/python3.6/lib-dynload',\n",
       " '',\n",
       " '/home/yinzp/.local/lib/python3.6/site-packages',\n",
       " '/usr/local/lib/python3.6/dist-packages',\n",
       " '/usr/lib/python3/dist-packages',\n",
       " '/home/yinzp/.local/lib/python3.6/site-packages/IPython/extensions',\n",
       " '/home/yinzp/.ipython']"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "import sys\n",
    "import torch\n",
    "module_dict = sys.modules\n",
    "module_dict['torch']"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<module 'torch' from '/home/yinzp/.local/lib/python3.6/site-packages/torch/__init__.py'>"
      ]
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 嵌套`import`\n",
    "### 1. 顺序嵌套\n",
    "例如：本模块导入 A 模块（import A），A 中又 import B，B 模块又可以 import 其他模块……\n",
    "这中嵌套比较容易理解，需要注意的一点就是各个模块的 Local 名字空间是独立的。对于上面的例子，本模块 import A 之后本模块只能访问模块 A，不能访问模块 B 及其他模块。虽然模块 B 已经加载到内存了，如果访问还要再明确的在本模块中 import B。"
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.6.9",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.9 64-bit"
  },
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}